package controle;

import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import abstraction.Cours;
import abstraction.Etudiant;

/** controleur qui gère l'affichage de la liste des étudiants
 * 
 * @author PICOSSON
 *
 */
public class ControlJListEtudiants implements Observer, ListSelectionListener {


	private JList<String> liste;
	private Cours modele;
	
	public ControlJListEtudiants(Cours modele, JList<String> liste) {
		this.modele = modele;
		this.liste = liste;		
		Vector<String> data = this.modele.getEtudiants();
		data.add(0," - ");
		this.liste.setListData(data);


		}

	public void valueChanged(ListSelectionEvent e) {
		if (this.liste.getSelectedIndex() > 0){
			this.modele.setEtudiantCourant(this.modele.findEtudiants().get(this.liste.getSelectedIndex()-1).getEmail());
		}else
			this.modele.setEtudiantCourant(null);
	}
		
	public void update(Observable o, Object message) {
		Integer iMessage = (Integer) message;
		if (iMessage == Cours.CHANGEMENT_MODULE_COURANT
				|| iMessage == Cours.CHANGEMENT_GROUPE_COURANT
				|| iMessage == Cours.CHANGEMENT_EVALUATION_COURANTE
				|| iMessage == Cours.CHANGEMENT_ETUDIANTS
				|| iMessage == Cours.CHANGEMENT_NOTES) {
			Vector<String> data = this.modele.getEtudiants();
			data.add(0," - ");
			this.liste.setListData(data);
			
			if(this.modele.getEtudiantCourant() != null){
				Vector<Etudiant> e = this.modele.findEtudiants();
				int i = 0;
				while(e.get(i).getEmail() != this.modele.getEtudiantCourant()){
					i++;
				}
				this.liste.setSelectedValue(this.modele.getEtudiants().get(i), true);
			}
			else{
				this.liste.setSelectedIndex(0);
			}	
		}
		this.liste.repaint();
	}

}
